﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using OpenNETCF.Bookshare.Entities;

namespace OpenNETCF.Bookshare.Services.ISBNDB
{
    internal class AuthorResponseParser : IResponseParser
    {
        public IResponse Parse(string xml)
        {
            var doc = XDocument.Parse(xml);

            foreach (var node in doc.Descendants("AuthorData"))
            {
                var b = new Book();

                var attrib = node.Attribute("isbn13");
                if (attrib != null)
                {
                    b.ISBN = attrib.Value;
                }

                var child = node.Descendants("Title").FirstOrDefault();
                if (child != null)
                {
                    b.Title = child.Value;
                }

                child = node.Descendants("AuthorsText").FirstOrDefault();
                if (child != null)
                {
                    var authors = child.Value.Split(new char[] { ',' });
                }

                child = node.Descendants("PublisherText").FirstOrDefault();
                if (child != null)
                {
                    // <PublisherText publisher_id="oreilly_media">O'Reilly Media</PublisherText>
                    // todo
                }

                child = node.Descendants("Details").FirstOrDefault();
                if (child != null)
                {
                    // <Details change_time="2009-11-11T16:14:06Z" price_time="2010-09-02T20:24:33Z" edition_info="Paperback; 2009-11-26" language="" physical_description_text="476 pages" lcc_number="" dewey_decimal_normalized="5" dewey_decimal="005"/>
                    // todo
                }

            }

            return new AuthorResponse();
        }
    }
}
